package com.imvc.util;

/**
 * 数据会话工厂
 *
 * @author ml
 * @create 2017-10-18--16:24
 */
public class DbSessionFactory {

    private static final ThreadLocal<DbSession> THREAD_LOCAL=new ThreadLocal<>();

    private DbSessionFactory(){

    }

    /**
     * 打开会话
     * @return dbsession对象
     */
    public static DbSession openSession(){
        DbSession dbSession=THREAD_LOCAL.get();

        if(dbSession==null){
            dbSession=new DbSession();
            THREAD_LOCAL.set(dbSession);
        }
        dbSession.open();
        return dbSession;
    }

    public static void closeSession(){
        DbSession dbSession=THREAD_LOCAL.get();
        THREAD_LOCAL.set(null);
        if(dbSession!=null){
            dbSession.close();
        }
    }

}
